package com.zhuchang.self_rpc.test.democoding;

import io.netty.buffer.ByteBuf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author RenPu
 * @Date 2022/2/7 16:35
 * @Version 1.0
 * @Description:将文字从文件中读取出来，输出到控制台
 *              利用NIO（面向块编程或者面向缓冲区编程）中的ByteBuffer,Channel,可以互相读写数据的特性
 **/
public class FileOutPutStream {

    public static void main(String[] args) throws IOException {
        File file = new File("d:\\test.txt");

        //创建输入流对象
        FileInputStream fileInputStream = new FileInputStream(file);

        //获取输入流所属的Channel对象
        FileChannel channel = fileInputStream.getChannel();

        //创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());

        //将Channel的数据写入到缓冲区
        channel.read(buffer);

        //将缓存区的字节数组转化为字符串，并打印出来
        System.out.println(new String(buffer.array()));

        //释放IO资源
        fileInputStream.close();

    }


}
